package com.linkcard.commen.tools;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Helper {

	// 乱码处理
	public static String decode(String text) {
		if (text == null) {
			return null;
		}
		try {
			return URLDecoder.decode(text, "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return null;
	}

	// 乱码处理
	public static String encode(String text) {
		if (text == null) {
			return null;
		}
		try {
			return URLEncoder.encode(text, "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return null;
	}

	// 乱码处理
	public static String convertToChinese(String str) {
		if (str == null) {
			return null;
		}
		String keyword = null;
		try {
			if (str != null) {
				keyword = new String(str.getBytes("ISO-8859-1"), "utf-8");
			}
		} catch (UnsupportedEncodingException e) {

			e.printStackTrace();
		}
		return keyword;
	}

	// 获得评论图标
	public static String criticismImage(int count) {
		String keyword = "";
		if (count == 0) {
			keyword = "<img  src='image/pingjia1.gif'/><img  src='image/pingjia1.gif'/><img  src='image/pingjia1.gif'/><img  src='image/pingjia1.gif'/><img  src='image/pingjia1.gif'/>";
		} else {
			for (int i = 0; i < 5; i++) {
				if ((i + 1) <= count) {
					keyword += "<img  src='image/pingjia2.gif'/>";
				} else {
					keyword += "<img  src='image/pingjia1.gif'/>";
				}
			}
		}

		return keyword;
	}

	// 去年html代码
	public static String deleteHtml(String content, int length, String str) {

		content = content.replaceAll("&nbsp;", "");
		content = content.replaceAll("\\<.*?>", "");
		if (length != 0) {
			if (content.length() > length) {
				content = content.substring(0, length) + str;
			}
		}
		return content;
	}

	// 获得日期
	public static String formatDate(String dateStr, int n) {

		SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
		Date date = null;
		try {
			date = myFormatter.parse(dateStr);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return myFormatter.format(new Date(date.getTime() + n * 24 * 60 * 60
				* 1000));
	}
	
}
